package com.enjoy.studyfragment.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.enjoy.mjbaseframe.util.GlideUtil;
import com.enjoy.studyfragment.R;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.orhanobut.logger.Logger;
import java.io.File;


/**
 * author：luck
 * project：PictureSelector
 * package：com.luck.pictureselector.adapter
 * email：893855882@qq.com
 * data：16/7/27
 */
public class CircleFriendsAdapter extends BaseQuickAdapter<LocalMedia, BaseViewHolder>{
    public static final int TYPE_CAMERA = 1;
    public static final int TYPE_PICTURE = 2;
    private int selectMax = 9;
    private Context context;

    public CircleFriendsAdapter(Context context){
        super(R.layout.adapter_circle_friends);
        this.context = context;
    }

    @Override
    protected void convert(final BaseViewHolder helper, LocalMedia item) {
        final ImageView mImg = helper.getView(R.id.fiv);
        LinearLayout ll_del = helper.getView(R.id.ll_del);

        //少于8张，显示继续添加的图标
        final int position = helper.getAdapterPosition();
        Logger.d("position:"+ position);
        if (getItemViewType(position) == TYPE_CAMERA) {
            mImg.setImageResource(R.mipmap.common_img_add_photos);
            mImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onFirstItemClick();
                }
            });
            ll_del.setVisibility(View.INVISIBLE);
        } else {
            ll_del.setVisibility(View.VISIBLE);
            ll_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
                    // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
                    if (position != RecyclerView.NO_POSITION) {
                        mData.remove(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position, mData.size());
                    }
                }
            });
            LocalMedia media = mData.get(position);
            int mimeType = media.getMimeType();
            String path = "";
            if (media.isCut() && !media.isCompressed()) {
                // 裁剪过
                path = media.getCutPath();
            } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                path = media.getCompressPath();
            } else {
                // 原图
                path = media.getPath();
            }
            // 图片
            if (media.isCompressed()) {
                Log.i("compress image result:", new File(media.getCompressPath()).length() / 1024 + "k");
                Log.i("压缩地址::", media.getCompressPath());
            }

            Log.i("原图地址::", media.getPath());
            if (media.isCut()) {
                Log.i("裁剪地址::", media.getCutPath());
            }
            if (mimeType == PictureMimeType.ofAudio()) {
                mImg.setImageResource(R.drawable.audio_placeholder);
            } else {
                GlideUtil.loadImageView(context,path,mImg);
            }
            //itemView 的点击事件
            if (mItemClickListener != null) {
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mItemClickListener.onItemClick(position, v);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        //限制个数
        if (mData.size() < selectMax) {
            return mData.size() + 1;
        } else {
            return mData.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    //判断第一个或者最后一个
    private boolean isShowAddItem(int position) {
        int size = mData.size() == 0 ? 0 : mData.size();
        return position == size;
    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
        void onFirstItemClick();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}
